//
// Created by fairy on 2025/7/9.
//
static void screenInit() {
    // 创建主容器（无父对象）
    static lv_obj_t *main_cont = lv_obj_create(nullptr);
    lv_obj_set_size(main_cont, 480, 320);
    lv_obj_set_style_bg_color(main_cont, lv_color_hex(0x0C1020), LV_PART_MAIN);
    lv_obj_set_style_border_width(main_cont, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_all(main_cont, 0, LV_PART_MAIN);

    // 标题栏
    static lv_obj_t *header = lv_obj_create(main_cont);
    lv_obj_set_size(header, 480, 40);
    lv_obj_align(header, LV_ALIGN_TOP_LEFT, 0, 0);
    lv_obj_set_style_bg_color(header, lv_color_hex(0x151E30), LV_PART_MAIN);
    lv_obj_set_style_border_width(header, 0, LV_PART_MAIN);

    static lv_obj_t *title = lv_label_create(header);
    lv_label_set_text(title, "DIGITAL OSCILLOSCOPE");
    lv_obj_align(title, LV_ALIGN_LEFT_MID, 15, 0);
    lv_obj_set_style_text_color(title, lv_color_hex(0x00D3FF), LV_PART_MAIN);
    lv_obj_set_style_text_font(title, &lv_font_SourceHanSerifSC_Regular_16, LV_PART_MAIN);

    // 时间显示
    static lv_obj_t *time_label = lv_label_create(header);
    lv_label_set_text(time_label, "12:45:36");
    lv_obj_align(time_label, LV_ALIGN_RIGHT_MID, -15, 0);
    lv_obj_set_style_text_color(time_label, lv_color_hex(0x8A9BBA), LV_PART_MAIN);
    lv_obj_set_style_text_font(time_label, &lv_font_SourceHanSerifSC_Regular_12, LV_PART_MAIN);

    // 波形显示区域
    static lv_obj_t *wave_area = lv_obj_create(main_cont);
    lv_obj_set_size(wave_area, 440, 200);
    lv_obj_align(wave_area, LV_ALIGN_TOP_LEFT, 20, 50);
    lv_obj_set_style_bg_color(wave_area, lv_color_hex(0x050A15), LV_PART_MAIN);
    lv_obj_set_style_border_width(wave_area, 1, LV_PART_MAIN);
    lv_obj_set_style_border_color(wave_area, lv_color_hex(0x1A2C4F), LV_PART_MAIN);
    lv_obj_clear_flag(wave_area, LV_OBJ_FLAG_SCROLLABLE);

    // 绘制网格线
    static lv_point_t v_line_points[2] = {{0,0}, {0,200}};
    static lv_point_t h_line_points[2] = {{0,0}, {440,0}};

    for (int i = 0; i < 11; i++) {
        static lv_obj_t *v_line;
        v_line = lv_line_create(wave_area);
        v_line_points[0].x = i * 44; v_line_points[1].x = i * 44;
        lv_line_set_points(v_line, v_line_points, 2);
        lv_obj_set_style_line_color(v_line, (i%5) ? lv_color_hex(0x0E1A30) : lv_color_hex(0x142340), LV_PART_MAIN);
        lv_obj_set_style_line_width(v_line, 1, LV_PART_MAIN);
    }

    for (int i = 0; i < 6; i++) {
        static lv_obj_t *h_line;
        h_line = lv_line_create(wave_area);
        h_line_points[0].y = i * 40; h_line_points[1].y = i * 40;
        lv_line_set_points(h_line, h_line_points, 2);
        lv_obj_set_style_line_color(h_line, (i%2) ? lv_color_hex(0x0E1A30) : lv_color_hex(0x142340), LV_PART_MAIN);
        lv_obj_set_style_line_width(h_line, 1, LV_PART_MAIN);
    }

    // 创建波形线（通道1）
    static lv_point_t wave_points1[220];
    static lv_obj_t *wave_line1 = lv_line_create(wave_area);
    lv_line_set_points(wave_line1, wave_points1, 220);
    lv_obj_set_style_line_color(wave_line1, lv_color_hex(0x00FFC8), LV_PART_MAIN);
    lv_obj_set_style_line_width(wave_line1, 2, LV_PART_MAIN);

    // 创建波形线（通道2）
    static lv_point_t wave_points2[220];
    static lv_obj_t *wave_line2 = lv_line_create(wave_area);
    lv_line_set_points(wave_line2, wave_points2, 220);
    lv_obj_set_style_line_color(wave_line2, lv_color_hex(0xFF007A), LV_PART_MAIN);
    lv_obj_set_style_line_width(wave_line2, 2, LV_PART_MAIN);

    // 状态信息栏
    static lv_obj_t *info_bar = lv_obj_create(main_cont);
    lv_obj_set_size(info_bar, 440, 50);
    lv_obj_align(info_bar, LV_ALIGN_BOTTOM_LEFT, 20, -10);
    lv_obj_set_style_bg_color(info_bar, lv_color_hex(0x151E30), LV_PART_MAIN);
    lv_obj_set_style_border_width(info_bar, 0, LV_PART_MAIN);

    // 通道信息标签
    static const char *info_fmt = "CH1: %.1fV  CH2: %.1fV | 1ms/div | Trig: 0.2V";
    static lv_obj_t *info_label = lv_label_create(info_bar);
    lv_label_set_text_fmt(info_label, info_fmt, 0.0, 0.0);
    lv_obj_align(info_label, LV_ALIGN_LEFT_MID, 10, 0);
    lv_obj_set_style_text_color(info_label, lv_color_hex(0x8A9BBA), LV_PART_MAIN);
    lv_obj_set_style_text_font(info_label, &lv_font_SourceHanSerifSC_Regular_12, LV_PART_MAIN);

    // 控制按钮
    static lv_obj_t *btn_run = lv_btn_create(info_bar);
    lv_obj_set_size(btn_run, 80, 30);
    lv_obj_align(btn_run, LV_ALIGN_RIGHT_MID, -10, 0);
    lv_obj_t *btn_label = lv_label_create(btn_run);
    lv_label_set_text(btn_label, "RUN");
    lv_obj_center(btn_label);
    lv_obj_set_style_text_font(btn_label, &lv_font_SourceHanSerifSC_Regular_12, LV_PART_MAIN);

    // 波形生成定时器（模拟数据）
    static lv_timer_t *wave_timer;
    wave_timer = lv_timer_create([](lv_timer_t *timer) {
        static float phase = 0.0f;
        phase += 0.1f;

        // 生成正弦波 (通道1)
        for (int i = 0; i < 220; i++) {
            wave_points1[i].x = i;
            float value = sinf(phase + i * 0.1f) * 0.8f;
            wave_points1[i].y = 100 + value * 80; // 缩放后居中显示
        }

        // 生成方波 (通道2)
        for (int i = 0; i < 220; i++) {
            wave_points2[i].x = i;
            float value = (fmodf(phase * 2 + i * 0.2f, 6.28f) < 3.14f) ? 0.6f : -0.6f;
            wave_points2[i].y = 100 + value * 80;
        }

        // 更新波形显示
        lv_line_set_points(wave_line1, wave_points1, 220);
        lv_line_set_points(wave_line2, wave_points2, 220);

        // 更新信息标签
        lv_label_set_text_fmt(info_label, info_fmt,
                              fabs(sinf(phase)) * 3.3f,
                              fabs(sinf(phase * 1.3f)) * 5.0f);
    }, 50, nullptr); // 20Hz刷新

    // 更新时间显示
    static lv_timer_t *time_timer;
    time_timer = lv_timer_create([](lv_timer_t *timer) {
        static uint8_t count = 0;
        lv_label_set_text_fmt(time_label, "%02d:%02d:%02d",
                              (count/3600)%24, (count/60)%60, count%60);
        count = (count + 1) % 86400;
    }, 1000, nullptr);

    // 完成初始化
    lv_obj_update_layout(main_cont);
    lv_scr_load(main_cont);
}